//
//  EditorViewController.m
//  shusheng
//
//  Created by rimi on 16/7/9.
//  Copyright © 2016年 yuying. All rights reserved.
//

#import "EditorViewController.h"
#import "NewEditorWord.h"
#import "NewCreatEssay.h"

#import "EssayOfNet.h"

#import <iflyMSC/IFlyRecognizerViewDelegate.h>
#import <iflyMSC/IFlySpeechRecognizerDelegate.h>
#import <iflyMSC/IFlySpeechSynthesizerDelegate.h>



#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVAudioSession.h>
#import <AudioToolbox/AudioSession.h>
#import <iflyMSC/IFlyDataUploader.h>
#import <iflyMSC/IFlySpeechUtility.h>
#import "iflyMSC/IFlySpeechConstant.h"

#import <iflyMSC/IFlyContact.h>
#import <iflyMSC/IFlyUserWords.h>
@class IFlySpeechError;
#import <Foundation/Foundation.h>


@interface EditorViewController ()

@property (nonatomic,strong) UIView *btnBview;
@property (nonatomic,assign) int numberOfFontBtn;
@property (nonatomic,assign) int numberOfColorBtn;
@property (nonatomic,assign) int numberOfLanguageBtn;
@property (nonatomic,assign) NSUInteger numberOfLocation;//聆听时光标位置

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewToBottom;
@property (nonatomic,assign)NSLayoutConstraint *layoutConstraint;

@property (nonatomic,strong) UITapGestureRecognizer *singleTapGestureRecognizer;

@end

@implementation EditorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _titleLabel.text = _titleNameSting;
    _textString.text = _wordString;
//    _numberOfFontBtn = 0;
//    _numberOfColorBtn = 0;
//    _numberOfLanguageBtn = 0;
    [self readUserInfomationBasic];
    
    
    [self keyboard];
    _layoutConstraint.constant = _textViewToBottom.constant;
   
    NSLog(@"=========%@",_textString.text);
    
    [self hecheng];//合成部分
    [self shibie];//识别部分
  
   
}

#pragma mark -- WillDisappear --
#pragma mark --

-(void)viewWillDisappear:(BOOL)animated{
    [_iFlySpeechUnderstander cancel];
    _iFlySpeechUnderstander.delegate = nil;
    //设置回非语义识别
    [_iFlySpeechUnderstander destroy];
    
    
    [self write];
    [self saveUserInfomationBasic];
    
    
    [super viewWillDisappear:animated];
    
}


- (void)hecheng{
    NSLog(@"--------------合成---------------");
    //创建合成对象，为单例模式
    _iFlySpeechSynthesizer = [IFlySpeechSynthesizer sharedInstance];
    _iFlySpeechSynthesizer.delegate = self;
    
    //设置语音合成的参数
    //合成的语速,取值范围 0~100
    [_iFlySpeechSynthesizer setParameter:@"30" forKey:[IFlySpeechConstant SPEED]];
    //合成的音量;取值范围 0~100
    [_iFlySpeechSynthesizer setParameter:@"50" forKey:[IFlySpeechConstant VOLUME]];
    //发音人,默认为”xiaoyan”;可以设置的参数列表可参考个性化发音人列表
    [_iFlySpeechSynthesizer setParameter:@"xiaoyan" forKey:[IFlySpeechConstant VOICE_NAME]];
    //音频采样率,目前支持的采样率有 16000 和 8000
    [_iFlySpeechSynthesizer setParameter:@"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
    ////asr_audio_path保存录音文件路径，如不再需要，设置value为nil表示取消，默认目录是documents
    [_iFlySpeechSynthesizer setParameter:@"tts.pcm" forKey:[IFlySpeechConstant TTS_AUDIO_PATH]];
    }


- (void)shibie{
    NSLog(@"--------------识别---------------");
    //创建识别对象
    //创建语音配置
    NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@,timeout=%@",@"578362f0",@"20000"];
    //所有服务启动前，需要确保执行createUtility
    [IFlySpeechUtility createUtility:initString];
    _iFlySpeechUnderstander = [IFlySpeechUnderstander sharedInstance];
    _iFlySpeechUnderstander.delegate = self;
    
}


#pragma mark -- 键盘 --
#pragma mark --

- (void)keyboard{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}


- (void)keyboardWillShow:(NSNotification *) notif{
    
    
    if (self.textString.selectedRange.location > 50) {
 
        [self.view layoutIfNeeded];
    
        NSDictionary *info = [notif userInfo];
        NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
        CGSize keyboardSize = [value CGRectValue].size;
         NSLog(@"keyBoard:%f", keyboardSize.height);
        _textViewToBottom.constant = keyboardSize.height;
       
    }
}

- (void)keyboardWillHide{
    [self.view layoutIfNeeded];
    _textViewToBottom.constant = _layoutConstraint.constant;
}

#pragma mark -- action --
#pragma mark --

- (IBAction)btn:(UIButton *)sender {
    //130moreBtn
    //131开始与结束
    switch (sender.tag - TAG_BTN) {
        case 10:
            
            sender.selected = !sender.selected;
            
            if (sender.selected) {
                [sender setTitle:@"收回" forState:UIControlStateNormal];
                [self moreBtn];
                _startOrEndBtn.hidden = YES;
                _btnBview.hidden = NO;
            }else{
                [sender setTitle:@"更多" forState:UIControlStateNormal];
                _btnBview.hidden = YES;
                _startOrEndBtn.hidden = NO;
            }

             break;
        case 11:
  
            //开始监听与结束监听
            _startOrEndBtn.selected = !_startOrEndBtn.selected;
            
            if (_startOrEndBtn.selected) {
                bool ret = [_iFlySpeechUnderstander startListening];  //开始监听
                [MBProgressHUD showSuccess:@"聆听中"];

                if (ret) {
                     self.isCanceled = NO;
                }else{
                     NSLog(@"启动识别失败!");
                     [MBProgressHUD showError:@"请查看是否联网"];
                     sender.selected = !sender.selected;
                }
                
            }else{
                [_iFlySpeechUnderstander stopListening];   //结束监听
                NSLog(@"结束监听!");
                _startOrEndBtn.selected = !_startOrEndBtn.selected;
            }
            break;
            
        default:
            break;
    }
    
}


#pragma mark -- morebtnview --
#pragma mark --

- (void)moreBtn{
    
    _btnBview = [[UIView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) -64)];
    _btnBview.backgroundColor = [UIColor whiteColor];
    _btnBview.backgroundColor = [_btnBview.backgroundColor colorWithAlphaComponent:0.1];
    
    [self.view addSubview:_btnBview];
    _btnBview.hidden = YES;
    
    
    
    NSArray *sideBtnArr = @[@"颜色",@"字体",@"朗读",@"语言",@"分享",@"网络同步"];
    for (int i = 0; i < sideBtnArr.count; i ++) {
        
        UIButton * sideBtn = [UIButton creatSideBtnCenter:CGPointMake(CGRectGetWidth(self.view.bounds)*0.9, 64+60*i) color:[UIColor redColor] title:sideBtnArr[i] tag:TAG_BTN+20+i];
        [sideBtn setTitle:sideBtnArr[i] forState:UIControlStateNormal];
        [sideBtn addTarget:self action:@selector(moreBtnActions:) forControlEvents:UIControlEventTouchUpInside];
        [_btnBview addSubview:sideBtn];
        
        [self singleTapGesture];
    }

}

#pragma mark -- 手势 --
#pragma mark --
- (void)singleTapGesture{
    _singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTapToBtnBackView:)];
    [_singleTapGestureRecognizer setNumberOfTapsRequired:1];
    [_btnBview addGestureRecognizer:_singleTapGestureRecognizer];
}

- (void)singleTapToBtnBackView:(UIGestureRecognizer *)gesture{

    NSLog(@"_btnBview点击事件");
    [_btnBview removeFromSuperview];
    _shareBtn.selected = !_shareBtn.selected;
    [_shareBtn setTitle:@"更多" forState:UIControlStateNormal];
    _startOrEndBtn.hidden = NO;
}

#pragma mark -- 基础设置的保存与使用 --
#pragma mark --
- (void)readUserInfomationBasic{
    _numberOfColorBtn = [[[NSUserDefaults standardUserDefaults] objectForKey:@"color"] intValue];
    _numberOfFontBtn = [[[NSUserDefaults standardUserDefaults] objectForKey:@"font"] intValue];
    _numberOfLanguageBtn = [[[NSUserDefaults standardUserDefaults] objectForKey:@"language"] intValue];
    [self basicViewOfColor];
    [self basicViewOfFont];
    [self basicViewOfLanguage];

}
// 保存用户信息
- (void)saveUserInfomationBasic{
    
    
    int color = _numberOfColorBtn%6;
    int font = _numberOfFontBtn%6;
    int language = _numberOfLanguageBtn%6;
    
    [[NSUserDefaults standardUserDefaults] setObject:@(color)forKey:@"color"];
    [[NSUserDefaults standardUserDefaults] setObject:@(font) forKey:@"font"];
    [[NSUserDefaults standardUserDefaults] setObject:@(language) forKey:@"language"];
    
    // 同步数据
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}

- (void)basicViewOfColor{
    switch (_numberOfColorBtn%6) {
        case 0:
            _textString.backgroundColor = [UIColor blackColor];
            _textString.textColor = [UIColor whiteColor];
            break;
        case 1:
            _textString.backgroundColor = [UIColor whiteColor];
            _textString.textColor = [UIColor blackColor];
            break;
        case 2:
            _textString.backgroundColor = COLOR_GRAY_W;
            _textString.textColor = [UIColor blackColor];
            break;
        case 3:
            _textString.backgroundColor = COLOR_YELLOW_W;
            _textString.textColor = [UIColor blackColor];
            break;
        case 4:
            _textString.backgroundColor = COLOR_GREEN_W;
            _textString.textColor = [UIColor blackColor];
            break;
        case 5:
            _textString.backgroundColor = COLOR_PINK_W;
            _textString.textColor = [UIColor blackColor];
            break;
            
        default:
            break;
    }

}

- (void)basicViewOfFont{
    switch (_numberOfFontBtn%6) {
        case 0:
            _textString.font = [UIFont systemFontOfSize:14];
            break;
        case 1:
            _textString.font = [UIFont systemFontOfSize:20];
            break;
        case 2:
            _textString.font = [UIFont systemFontOfSize:24];
            break;
        case 3:
            _textString.font = [UIFont systemFontOfSize:28];
            break;
        case 4:
            _textString.font = [UIFont systemFontOfSize:35];
            break;
        case 5:
            _textString.font = [UIFont systemFontOfSize:45];
            break;
            
        default:
            break;
    }
}

- (void)basicViewOfLanguage{
    switch (_numberOfLanguageBtn%6) {
        case 0://中文普通话
            [_iFlySpeechUnderstander setParameter:@"zh_cn" forKey:[IFlySpeechConstant LANGUAGE]];//中国
            [_iFlySpeechSynthesizer setParameter:@"xiaoyan" forKey:[IFlySpeechConstant VOICE_NAME]];
            [MBProgressHUD showSuccess:@"中文模式"];
            break;
        case 1://中文四川话
            [_iFlySpeechUnderstander setParameter:@"lmz" forKey:[IFlySpeechConstant ACCENT]];
            [_iFlySpeechSynthesizer setParameter:@"aisxrong" forKey:[IFlySpeechConstant VOICE_NAME]];
            break;
        case 2://中文粤语
            [_iFlySpeechUnderstander setParameter:@"cantonese" forKey:[IFlySpeechConstant ACCENT]];//中国
            [_iFlySpeechSynthesizer setParameter:@"xiaomei" forKey:[IFlySpeechConstant VOICE_NAME]];
            break;
            
        case 3://河南
            [_iFlySpeechUnderstander setParameter:@"henanese" forKey:[IFlySpeechConstant ACCENT]];//中国
            [_iFlySpeechSynthesizer setParameter:@"aisxkun" forKey:[IFlySpeechConstant VOICE_NAME]];
            break;
        case 4://台湾
            [_iFlySpeechUnderstander setParameter:@"zh_tw" forKey:[IFlySpeechConstant LANGUAGE]];//中国
            [_iFlySpeechSynthesizer setParameter:@"aisxlin" forKey:[IFlySpeechConstant VOICE_NAME]];
            
            break;
        case 5://英文
            [_iFlySpeechUnderstander setParameter:@"en_us" forKey:[IFlySpeechConstant LANGUAGE]];//中国
            [_iFlySpeechSynthesizer setParameter:@"Catherine" forKey:[IFlySpeechConstant VOICE_NAME]];
            [MBProgressHUD showSuccess:@"英文模式"];
            break;
            
            
        default:
            break;
    }
    
}
#pragma mark -- morebtnaction --
#pragma mark --

- (void)moreBtnActions:(UIButton *)sender{
    
    switch (sender.tag - TAG_BTN) {
        case 20:
            _numberOfColorBtn += 1;
            //颜色字体部分
            
            
            switch (_numberOfColorBtn%6) {
                case 0:
                    [sender setTitle:@"夜间" forState:UIControlStateNormal];
                    _textString.backgroundColor = [UIColor blackColor];
                    _textString.textColor = [UIColor whiteColor];
                    break;
                case 1:
                    [sender setTitle:@"白天" forState:UIControlStateNormal];
                    _textString.backgroundColor = [UIColor whiteColor];
                    _textString.textColor = [UIColor blackColor];
                    break;
                case 2:
                    [sender setTitle:@"灰白" forState:UIControlStateNormal];
                    _textString.backgroundColor = COLOR_GRAY_W;
                    _textString.textColor = [UIColor blackColor];
                    break;
                case 3:
                    [sender setTitle:@"旧书" forState:UIControlStateNormal];
                    _textString.backgroundColor = COLOR_YELLOW_W;
                    _textString.textColor = [UIColor blackColor];
                    break;
                case 4:
                    [sender setTitle:@"护眼" forState:UIControlStateNormal];
                    _textString.backgroundColor = COLOR_GREEN_W;
                    _textString.textColor = [UIColor blackColor];
                    break;
                case 5:
                    [sender setTitle:@"浪漫" forState:UIControlStateNormal];
                    _textString.backgroundColor = COLOR_PINK_W;
                    _textString.textColor = [UIColor blackColor];
                    break;
                    
                default:
                    break;
            }
            
            
            
            break;
        case 21:
            _numberOfFontBtn += 1;
            switch (_numberOfFontBtn%6) {
                case 0:
                    _textString.font = [UIFont systemFontOfSize:14];
                    NSLog(@"14");
                    break;
                case 1:
                    _textString.font = [UIFont systemFontOfSize:20];
                    NSLog(@"20");
                    break;
                case 2:
                    _textString.font = [UIFont systemFontOfSize:24];
                    NSLog(@"24");
                    break;
                case 3:
                    _textString.font = [UIFont systemFontOfSize:28];
                    NSLog(@"28");
                    break;
                case 4:
                    _textString.font = [UIFont systemFontOfSize:35];
                    NSLog(@"35");
                    break;
                case 5:
                    _textString.font = [UIFont systemFontOfSize:45];
                    NSLog(@"45");
                    break;
                    
                default:
                    break;
            }
            break;
        case 22:
             sender.selected = !sender.selected;
            if (sender.selected) {
                NSLog(@"开始合成");
                [sender setTitle:@"结束" forState:UIControlStateNormal];
                [_iFlySpeechSynthesizer startSpeaking:self.textString.text];
     
            }else{
                NSLog(@"停止合成");
                [_iFlySpeechSynthesizer stopSpeaking];
                [sender setTitle:@"朗读" forState:UIControlStateNormal];
            }
            
            break;
        case 23:
            NSLog(@"语言");
 
            /*方言普通话(mandarin)、英语(en_us)、粤语(cantonese)、四川话(lmz)、河南话(henanese)
             */
            _numberOfLanguageBtn += 1;
            switch (_numberOfLanguageBtn%6) {
                case 0://中文普通话
                    [_iFlySpeechUnderstander setParameter:@"zh_cn" forKey:[IFlySpeechConstant LANGUAGE]];//中国
                    [_iFlySpeechSynthesizer setParameter:@"xiaoyan" forKey:[IFlySpeechConstant VOICE_NAME]];
                    [MBProgressHUD showSuccess:@"中文模式"];
                    [sender setTitle:@"普通话" forState:UIControlStateNormal];
                    
                    break;
                case 1://中文四川话
                    [_iFlySpeechUnderstander setParameter:@"lmz" forKey:[IFlySpeechConstant ACCENT]];
                    [_iFlySpeechSynthesizer setParameter:@"aisxrong" forKey:[IFlySpeechConstant VOICE_NAME]];
                    [sender setTitle:@"四川话" forState:UIControlStateNormal];
                    break;
                case 2://中文粤语
                    [_iFlySpeechUnderstander setParameter:@"cantonese" forKey:[IFlySpeechConstant ACCENT]];//中国
                    [_iFlySpeechSynthesizer setParameter:@"xiaomei" forKey:[IFlySpeechConstant VOICE_NAME]];
                    [sender setTitle:@"粤语" forState:UIControlStateNormal];
                    break;
                    
                case 3://河南
                    [_iFlySpeechUnderstander setParameter:@"henanese" forKey:[IFlySpeechConstant ACCENT]];//中国
                    [_iFlySpeechSynthesizer setParameter:@"aisxkun" forKey:[IFlySpeechConstant VOICE_NAME]];
                    [sender setTitle:@"河南话" forState:UIControlStateNormal];
                    break;
                case 4://台湾
                    [_iFlySpeechUnderstander setParameter:@"zh_tw" forKey:[IFlySpeechConstant LANGUAGE]];//中国
                    [_iFlySpeechSynthesizer setParameter:@"aisxlin" forKey:[IFlySpeechConstant VOICE_NAME]];
                    [sender setTitle:@"台湾话" forState:UIControlStateNormal];

                    break;
                case 5://英文
                    [_iFlySpeechUnderstander setParameter:@"en_us" forKey:[IFlySpeechConstant LANGUAGE]];//中国
                    [_iFlySpeechSynthesizer setParameter:@"Catherine" forKey:[IFlySpeechConstant VOICE_NAME]];
                    [MBProgressHUD showSuccess:@"英文模式"];
                    [sender setTitle:@"英语" forState:UIControlStateNormal];
                    break;
                
                    
                default:
                    break;
            }
            
            
            break;
        case 24:
            NSLog(@"4");
            //分享
            [MBProgressHUD showSuccess:@"暂无分享功能"];
            break;
        case 25:
            NSLog(@"网络同步状态");
            //查询
            //已同步——弹窗
            //未同步--同步操作

            if (_isNovel) {
                NSLog(@"小说暂未有网络同步操作");
                [MBProgressHUD showSuccess:@"小说暂未有网络同步操作"];
            }else{
                
                NSString *objectId = [NewCreatEssay readObjectIdOfEssayId:self.essayId];
                NSLog(@"___________%@__________",objectId);
                if (objectId.length > 0) {
                    
                    NSLog(@"____1__文章已存在，更新进行中______");
                    NSString *str = [NSString stringWithFormat:@"文章%@已存在，更新进行中",self.titleNameSting];
                    [MBProgressHUD showSuccess:str];
                    //                    //更新
//                    //查询对应essayid-》objectid
//                    NSString *objectId = [NewCreatEssay readObjectIdOfEssayId:self.essayId];
                    //然后更新
                    [EssayOfNet updataOneEssayOfObjectId:objectId essayString:self.textString.text];
                }else{
                    NSLog(@"____0___未同步，同步进行中_______");
                    [MBProgressHUD showSuccess:@"未同步，同步进行中"];
                    //同步网络
                    NSDictionary *dic = [[NSDictionary alloc]initWithObjects:@[_essayId,self.titleNameSting,[ToolOfBasic nowTime],self.textString.text] forKeys:@[@"essayID",@"essayName",@"time",@"essayString"]];
                    
                    [EssayOfNet essaySynchronizationOfNetWork:dic];//同步
                }
                
                
                
                //文章
//                if ([EssayOfNet readOfNetWorkOneEssayID:self.essayId]) {
//                     NSLog(@"____1__文章已存在，更新进行中______");
//                    NSString *str = [NSString stringWithFormat:@"文章%@已存在，更新进行中",self.titleNameSting];
//                    [MBProgressHUD showSuccess:str];
////                    //更新
//                    //查询对应essayid-》objectid
//                   NSString *objectId = [NewCreatEssay readObjectIdOfEssayId:self.essayId];
//                    //然后更新
//                    [EssayOfNet updataOneEssayOfObjectId:objectId essayString:self.textString.text];
//    
//                }else{
//                    NSLog(@"____0___未同步，同步进行中_______");
//                    [MBProgressHUD showSuccess:@"未同步，同步进行中"];
//                    //同步网络
//                    NSDictionary *dic = [[NSDictionary alloc]initWithObjects:@[_essayId,self.titleNameSting,[ToolOfBasic nowTime],self.textString.text] forKeys:@[@"essayID",@"essayName",@"time",@"essayString"]];
//                    
//                    [EssayOfNet essaySynchronizationOfNetWork:dic];//同步
//                    
//                }
                
            }
            
            
            break;
            
        default:
            break;
    }
    
    
    
}


#pragma mark -- 声音 --
#pragma mark --

- (void) onCompleted:(IFlySpeechError*) error{
    
    NSLog(@"%@",error);
}


#pragma mark - IFlySpeechRecognizerDelegate
/**
 * @fn      onVolumeChanged
 * @brief   音量变化回调
 * @param   volume      -[in] 录音的音量，音量范围1~100
 * @see
 */
- (void) onVolumeChanged: (int)volume{
//    NSLog(@"录音的音量=%d",volume);
    //录音的音量，音量范围1~100
}
/**
 * @fn      onBeginOfSpeech
 * @brief   开始识别回调
 * @see
 */
- (void) onBeginOfSpeech{
    NSLog(@"正在录音");
}
/**
 * @fn      onEndOfSpeech
 * @brief   停止录音回调
 * @see
 */
- (void) onEndOfSpeech{
    
    NSLog(@"停止录音");
    _startOrEndBtn.selected = !_startOrEndBtn.selected;
    [MBProgressHUD showSuccess:@"已停止语音记录"];
    
}
/**
 * @fn      onError
 * @brief   识别结束回调
 * @param   errorCode   -[out] 错误类，具体用法见IFlySpeechError
 */

- (void) onError:(IFlySpeechError*)errorCode{
    
    NSString *text = @"";
    if (self.isCanceled) {
        text = @"识别取消";
    }else if (errorCode == 0 ){
        if (_result.length == 0) {
            text = @"无识别结果";
        }else{
            text = @"识别成功";
        }
    }else{
        NSLog(@"%@",text);
    
    }
}

/**
 * @fn      onResults
 * @brief   识别结果回调
 * @param   result      -[out] 识别结果，NSArray的第一个元素为NSDictionary，NSDictionary的key为识别结果，value为置信度
 * @see
 */
- (void) onResults:(NSArray *) results isLast:(BOOL)isLast{
    NSArray * temp = [[NSArray alloc]init];
    NSString * str = [[NSString alloc]init];
    NSMutableString *result = [[NSMutableString alloc] init];
    NSDictionary *dic = results[0];
    for (NSString *key in dic) {
        [result appendFormat:@"%@",key];
        
    }
    NSLog(@"听写结果：%@",result);
    //---------讯飞语音识别JSON数据解析---------//
    NSError * error;
    NSData * data = [result dataUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"data: %@",data);
    NSDictionary * dic_result =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
    NSArray * array_ws = [dic_result objectForKey:@"ws"];
    //遍历识别结果的每一个单词
    for (int i=0; i<array_ws.count; i++) {
        temp = [[array_ws objectAtIndex:i] objectForKey:@"cw"];
        NSDictionary * dic_cw = [temp objectAtIndex:0];
        str = [str  stringByAppendingString:[dic_cw objectForKey:@"w"]];
        NSLog(@"识别结果:%@",[dic_cw objectForKey:@"w"]);
    }
    NSLog(@"最终的识别结果:%@",str);
    
    NSLog(@"-------------聆听时光标位置%lu,%lu",(unsigned long)_textString.selectedRange.length,(unsigned long)_textString.selectedRange.location);
    _numberOfLocation = _textString.selectedRange.location;

    
    //判断拼接~分割 连接
    if (self.textString.text.length > _numberOfLocation) {
        
        NSString *frontString = [self.textString.text substringToIndex:_numberOfLocation];
        NSString *backString = [self.textString.text substringFromIndex:_numberOfLocation];
        self.textString.text = [NSString stringWithFormat:@"%@%@%@",frontString,str,backString];

        //光标保留位置
        self.textString.selectedRange = NSMakeRange(_numberOfLocation+str.length, 0);
        NSLog(@"%lu",(unsigned long)self.textString.selectedRange.length);
    }else{
        //显示
        self.textString.text = [NSString stringWithFormat:@"%@%@",self.textString.text,str];
        
    }
    //存储
    [self write];
   
 
//    //去掉识别结果最后的标点符号
//    if ([str isEqualToString:@"。"] || [str isEqualToString:@"？"] || [str isEqualToString:@"！"]) {
//        NSLog(@"末尾标点符号：%@",str);
//        
//    }else{
//        //显示
//        self.textString.text = [NSString stringWithFormat:@"%@。%@",self.textString.text,str];
//        //存储
//        [self write];
//        
//    }
    
    
    _result = str;
}
#pragma mark -- end --
#pragma mark --


#pragma mark -- string --
#pragma mark --
//保存按钮本地
- (void)write{
    [NewEditorWord writeToOneTxt:_fileName words:_textString.text];
}




////实时保存到服务器
//- (void)wordSave:(NSString *)wordString{
//    NSLog(@"%@",_fileName);
//}


#pragma mark -- 返回btn --
#pragma mark --

- (IBAction)responBtn:(UIButton *)sender {
    NSLog(@"返回");
    [self write];
    [self saveUserInfomationBasic];
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
}

@end
